<template>
  <div class="page_main">
    <el-form ref="searchFrom" :inline="true" :model="searchFrom" size="mini">
      <search-form-box :show-more-btn="false">
        <template slot="showSearch">
          <el-form-item label="年" prop="year">
            <el-date-picker
              v-model="searchFrom.year"
              type="year"
              placeholder="选择年"
              value-format="yyyy"
            >
            </el-date-picker>
          </el-form-item>
        </template>
        <template slot="searchBtn">
          <el-form-item>
            <el-button size="mini" type="primary" plain @click="search">查询</el-button>
            <el-button size="mini" type="info" plain @click="resetForm">重置</el-button>
          </el-form-item>
        </template>
      </search-form-box>
    </el-form>
    <el-button size="mini" type="primary" @click="add()">添加</el-button>
    <el-table v-loading="tableLoading" :data="tableData" style="margin-top: 10px" stripe border size="mini">
      <el-table-column label="年" prop="year" align="center" width="80"></el-table-column>
      <el-table-column label="年度" align="center" width="100">
        <template slot-scope="scope">
          {{ scope.row.type === 1 ? '上半年' : '下半年' }}
        </template>
      </el-table-column>
      <el-table-column label="总额(万)" prop="target" align="center" width="100"></el-table-column>
      <el-table-column label="人员" align="center" min-width="200">
        <template slot-scope="scope">
          <div v-for="user in scope.row.userYearTargetList" :key="user.user_id">
            {{ user.name + '：' + user.target + '万' }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="100" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-edit-outline" @click="add(scope.row.id||1)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="searchFrom.page"
      :page-sizes="[10,20,50,100, 200, 300, 400]"
      :page-size.sync="searchFrom.page_size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import SearchFormBox from '@/components/SearchFormBox'
import { generateUUID } from '@/utils/util'
import { page } from '@/api/workbench/saleYearTarget'

export default {
  name: 'annual_plan_list',
  components: {
    SearchFormBox
  },
  data() {
    return {
      searchFrom: {
        page: 1,
        page_size: 10,
        year: null
      },
      listParams: {
        page: 1,
        page_size: 10,
        year: null
      },
      tableData: [],
      total: 0,
      tableLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    search() {
      this.searchFrom.page = 1
      this.listParams = Object.assign({}, this.searchFrom)
      this.getList()
    },
    resetForm() {
      this.$refs.searchFrom.resetFields()
      this.listParams = Object.assign({}, this.searchFrom)
      this.getList()
    },
    add(id) {
      const uuid = generateUUID()
      if (id) {
        this.$store.commit('keep/PUT_PAGE_PARAMS', {
          uuid: uuid,
          data: {
            id: id
          }
        })
      } else {
        this.$store.commit('keep/PUT_PAGE_PARAMS', {
          uuid: uuid,
          data: {
            is_create: true
          }
        })
      }
      this.$router.push({
        name: 'semi_annual_plan_edit',
        query: {
          uuid
        }
      })
    },
    handleSizeChange(val) {
      this.listParams.page_size = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listParams.page = val
      this.getList()
    },
    getList() {
      this.tableLoading = true
      page(this.listParams).then(res => {
        this.tableLoading = false
        this.tableData = res.data.records
        this.total = res.data.total
      }).catch(() => {
        this.tableLoading = false
      })
    }
  }
}
</script>

<style scoped>
.page_main {
  padding: 20px;
}
</style>
